package md.mru.core.tasks;

import md.mru.core.dtos.OrderDto;
import md.mru.core.service.FolderService;
import md.mru.core.service.OrderService;
import md.mru.frontend.utils.FolderStateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;
import java.util.Set;

/**
 * @author sergiu.marin
 * @since 10/07/2014.
 */

@Component
@EnableScheduling
public class ChangeFolderStateTask {

    Logger logger = LoggerFactory.getLogger(ChangeFolderStateTask.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private FolderService folderService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        logger.info("ChangeFolderStateTask successfully started at: {}", Calendar.getInstance().getTime());
        Set<Long> folderStateIds = FolderStateType.getExpiresFolderStatesIds();
        List<OrderDto> orders = orderService.loadOrderWithExpiredFolderStates(folderStateIds);
        folderService.changeFolderStates(orders);
    }
}
